﻿using UnityEngine;
using System.Collections;
using JSON;
using System.IO;
namespace AssetBundleHelper{
	public class AssetsVersionLoader : MonoBehaviour {
		public static AssetsVersionLoader Instance;
		System.Action finishCallback;
		JSONNode fileNode;
		public void Awake(){
			Instance = this;
		}
		public void Finish(){
			Debug.Log ("finish");
			finishCallback ();
			Instance = null;
			if(gameObject != null)
			GameObject.Destroy (gameObject);
		}
		public void Load(string version,System.Action<string,string,float> processcallback,System.Action _finishCallback,System.Action<WWW> _downloadCallback){
			finishCallback = _finishCallback;
			Debug.Log (string.Format("准备下载版本:{0}",version));
			AssetsLoader.Instance.LoadWWW (version + ".upk", delegate(WWW obj) {
				File.WriteAllBytes(LoadConf.AssetBundlePath+version+".upk",obj.bytes);
				LZMAUnit.UnPackFolder(LoadConf.AssetBundlePath+version,LoadConf.AssetBundlePath,delegate(long fileSize, long processSize) {
					var f = ABUnit.CountSize(fileSize);
					var p = ABUnit.CountSize(processSize);
					var precent = (float)((double)processSize/fileSize);
					processcallback(f,p,precent);
					if(precent == 1){
						Finish();
					}
					else{
						if(fileSize == 0)
							Finish();
					}
				});
			},_downloadCallback);
		}

		void StartLoad(){
			for (var i = 0; i < fileNode.Count; i++) {
				LoadAssetBundle (fileNode [i].ToString ());
			}
		}
		void LoadAssetBundle(string fileName){
				
		}

	}
}